package com.sp2p.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.PriorityQueue;

import com.sp2p.entity.Avatar;

public class RandomWeightUtil {
	public static void main(String[] args) {
        HashMap<String, Double> constans = new HashMap<String, Double>();
        constans.put("a", 70.0000);
        constans.put("b", 22.3250);
        constans.put("c", 1.0000);
        constans.put("d", 5.0000);
        constans.put("e", 1.0000);
        constans.put("f", 0.5000);
        constans.put("g", 0.0500);
        constans.put("h", 0.0250);
        constans.put("i", 0.0125);
        constans.put("j", 0.0500);
        constans.put("k", 0.0250);
        constans.put("l", 0.0125);

        ArrayList<Avatar> list = new ArrayList<Avatar>();
        list.add(new Avatar("a", constans.get("a"),""));
        list.add(new Avatar("b", constans.get("b"),""));
        list.add(new Avatar("c", constans.get("c"),""));
        list.add(new Avatar("d", constans.get("d"),""));
        list.add(new Avatar("e", constans.get("e"),""));
        list.add(new Avatar("f", constans.get("f"),""));
        list.add(new Avatar("g", constans.get("g"),""));
        list.add(new Avatar("h", constans.get("h"),""));
        list.add(new Avatar("i", constans.get("i"),""));
        list.add(new Avatar("j", constans.get("j"),""));
        list.add(new Avatar("k", constans.get("k"),""));
        list.add(new Avatar("l", constans.get("l"),""));
        
        int number = 10000;
        for (int i = 0; i < number; i++) {
        	Avatar[] result = getRandomN(list, 1);
            for (Avatar av : result) {
            	if(av.key.equals("e")){
            	System.out.println("key: " + av.key);}
            }
        }
 
    }
 
    public static Avatar[] getRandomN(ArrayList<Avatar> list, int count) {
        PriorityQueue<Avatar> queue = new PriorityQueue<Avatar>(count);
        for (Avatar a : list) {
            Avatar avatar = new Avatar(a.key, a.weight * Math.random(),a.remarks);
            queue.add(avatar);
            if (queue.size() > count) {
                queue.poll();
            }
        }
        Avatar[] result = new Avatar[count];
        for (int j = 0; j < count; j++) {
            result[j] = queue.poll();
        }
        return result;
    }
}
